package com.igeek.ch02;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * TODO
 *
 * @author xuxin
 * @since 2024/5/20
 */

public class BeanDefinitionTest {
    public static void main(String[] args) {
        //1.获取BeanDefinition对象
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition.setBeanClass(Person.class);
        beanDefinition.setScope("singleton");

        //2.创建IOC容器
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

        //3.将bean实例注入至IOC容器中
        ac.registerBeanDefinition("person",beanDefinition);
        ac.refresh();

        //4.从IOC容器中获取bean实例，并使用
        Person person = ac.getBean("person", Person.class);
        person.setName("王五");
        person.setAge(20);
        System.out.println(person);
    }
}
